package com.atguigu.gulimall.search.app;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.to.es.SkuEsModel;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSearchController {

    @Autowired
    private  ProductSaveService productSaveService;
    @PostMapping("/product/up")
    public R ProductSave(@RequestBody List<SkuEsModel> skuEsModelList) throws IOException {
        Boolean b = false;

        try{
            b = productSaveService.ProductSave(skuEsModelList);
        }catch (Exception e){
            // 出现这种错误一般是Es客户端连不上了之类的错误
            log.error("ElasticSearchController商品上架出错:{}" + e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }

        if(!b){
            return R.ok();
        }else{
            // 出现这样的错误才是说明我们某个sku的数据是真的有问题了
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }

    }

}
